package nku.csc460;

import java.net.*;
import java.io.*;
import java.util.*;

class fooclient
{
	Socket MyClient = null;
	boolean connection = false;
	DataInputStream inputStream = null;
    BufferedReader input = null;
    PrintWriter output = null;

	
	public fooclient()
	{
		startConnection();
		openInputStream();
		openOutputStream();
	}
	
	public static void main(String... Args)
	{
		final fooclient clientInstance = new fooclient();	
		boolean flag = false;
		System.out.println("Welcome to the foo game! ");
		System.out.print("Please enter your name: ");
		clientInstance.sendName();
		int i = java.lang.Integer.parseInt(clientInstance.readLine());
		for(; i < 10; i++)
		{
			flag = false;
			System.out.println(clientInstance.readLine());
			System.out.println("\t1)" + clientInstance.readLine());
			System.out.println("\t2)" + clientInstance.readLine());
			System.out.println("\t3)" + clientInstance.readLine());
			System.out.println("\t4)" + clientInstance.readLine());
			System.out.print("Please enter an answer in 10 seconds: ");
		/*	Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {
					clientInstance.sendAnswers();  // here is your UI method
				}
   			});
  			thread.start();
  			long endTimeMillis = System.currentTimeMillis() + 10000;
			while (thread.isAlive()) {
	  			if (System.currentTimeMillis() > endTimeMillis) {
	  				flag = true;
					break;
	  			}
	  			try {
					Thread.sleep(500);
  				}
  				catch(Exception e){
  					System.out.println(e);
  				}

			}
			if(flag == true)
			{
				thread.stop();
				clientInstance.sendMessage("");
			}
			for (int j=0; j<25; ++j) 
				System.out.println();
			if(flag == true)
			{
				System.out.println("\nYour time is up!");
				System.out.println("You didn't hit return!");
			}*/
			clientInstance.sendAnswers();			System.out.println(clientInstance.readLine());
			System.out.println(clientInstance.readLine());
			System.out.println(clientInstance.readLine());
		}
		System.out.println(clientInstance.readLine());
		System.out.println(clientInstance.readLine());
		System.out.println(clientInstance.readLine());
		System.out.println(clientInstance.readLine());
		System.out.println(clientInstance.readLine());
		System.out.println(clientInstance.readLine());
		System.out.println(clientInstance.readLine());
		System.out.println(clientInstance.readLine());
		
		clientInstance.cleanUp();
		System.exit(0);
	}
	
	public void startConnection()
	{
		while(connection == false)
		{
			try
			{
				MyClient = new Socket("localhost", 8241);
				connection = true;
				System.out.println("Conneted to game server!");
			}
			catch(IOException e)
			{
				System.out.println(e);
				System.out.println("Connection to game server failed!");
				System.exit(1);
			}			
		}
	}
	
	public void openInputStream()
	{
		try
		{
			inputStream = new DataInputStream(MyClient.getInputStream());
			input = new BufferedReader(new InputStreamReader(inputStream));
			System.out.println("The input stream is opened to recieve messages from client!");
		}
		catch(IOException e)
		{
			System.out.println(e);
			System.out.println("Failed to open an input stream to recieve messages from client!");
			System.exit(1);
		}
	}
	
	public void openOutputStream()
	{
		try 
		{
			output = new PrintWriter(MyClient.getOutputStream());
			System.out.println("The output stream is opened to send messages to client!");
		}
		catch(IOException e)
		{
			System.out.println(e);
			System.out.println("Failed to open stream to send messages to client!");
			System.exit(1);
		} 
	}
	
	//Method to send messages to client
    public void sendMessage(String... lines){
    	for(String line: lines)
    	{
    		output.println(line);
    	}
    	output.flush();
    }
    
    public void sendAnswers(){
    	BufferedReader commandLine = new BufferedReader(new InputStreamReader(System.in));
    	String answer = null;
    	try{
    		answer = commandLine.readLine();
    	}
    	catch(IOException e){
    		System.out.println(e);
    		System.out.println("Failed to read answer... Shutting down!");
    	}
    	this.validateAnswer(answer);
    }
    
    public void sendName(){
    	BufferedReader commandLine = new BufferedReader(new InputStreamReader(System.in));
    	String answer = null;
    	try{
    		answer = commandLine.readLine();
    	}
    	catch(IOException e){
    		System.out.println(e);
    		System.out.println("Failed to read answer... Shutting down!");
    	}
    	this.sendMessage(answer);
    }
    
    public void validateAnswer(String answer){
    	HashSet tempSet = new HashSet();
    	if(answer.length() > 3)
    	{
    		System.out.println("You tried to fuckin cheat! Your done, fuck off!");
    		System.exit(1);
    	}
    	else{
    		for(char temp: answer.toCharArray())
    		{
    			if((temp != '1') && (temp != '2') && (temp != '3') && (temp != '4'))
    			{
    				System.out.println("You tried to fuckin cheat! Your done, fuck off!");
    				System.exit(1);
    			}
    			if(!tempSet.contains(temp)){
    				tempSet.add(temp);
    			}
    			else{
    				System.out.println("You tried to fuckin cheat! Your done, fuck off!");
    				System.exit(1);
    			}
    		}
    	}
    	this.sendMessage(answer);
    }
    
    //Method to read messages from client
    public String readLine(){
    	String line = null;
    	try
    	{
    		line = input.readLine();	
		}
		catch(IOException e)
		{
			System.out.println(e);
			System.out.println("Communication Failed!");
			System.exit(1);
		}
		return line;
    } 
    	
    public void cleanUp(){
    	this.output.flush();
    	this.MyClient = null;
		this.inputStream = null;
	    this.input = null;
	    this.output = null;
    }
}